package com.neusoft.elm.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.util.DBUtil;

public class BusinessDaoImpl implements BusinessDao {
	 Connection con = null;
	 PreparedStatement pst = null;
	 ResultSet rs = null;
	 
	 
	public List<Business> listBusiness(String businessName,String businessAddress) {//返回数据类型是对象型的 下面生成一个null
		//String sql="SELECT * FROM business WHERE 1=1";
		StringBuffer sql=new StringBuffer("SELECT * FROM business WHERE 1=1 ");
		//商家名称
		if(businessName!=null&&!businessName.equals("")){
			sql.append("AND businessName LIKE '%"+ businessName +"%'");
			
		}
		//商家地址
		if(businessName!=null&&!businessAddress.equals("")){
			sql.append("AND businessAddress LIKE '%"+ businessAddress +"%'");
		}
		List<Business> list = new ArrayList<>();    //business对象放List里面
		//连接数据库
		//接一下数据库 本来在try外面 直接扔里面
		try {
			con= DBUtil.getConnection();
			pst=con.prepareStatement(sql.toString());
			rs=pst.executeQuery();//rs接收一下
			while(rs.next()){ 
				Business business = new Business();
				business.setBusinessAddress(rs.getString("businessAddress"));//set放数据
			    business.setBusinessExplain(rs.getString("businessExplain"));
			    business.setBusinessId(rs.getInt("businessId"));
			    business.setBusinessName(rs.getString("businessName"));
			    business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
			    business.setPassword(rs.getString("password"));
			    business.setStarPrice(rs.getDouble("starPrice"));
			    list.add(business);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(rs, pst, con);
		}//关闭连接必须加
		return list;//自己改成list
	}

}
